package com.das.mechanic_base.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.VibrateHelp;

public class X3KeyBoardDialog extends Dialog implements View.OnClickListener {
    private Point mScreenPoint = new Point();
    private TextView number_1;
    private TextView number_2;
    private TextView number_3;
    private TextView number_4;
    private TextView number_5;
    private TextView number_6;
    private TextView number_7;
    private TextView number_8;
    private TextView number_9;
    private TextView number_0;
    private RelativeLayout rl_delete;
    private RelativeLayout rl_add;
    private TextView tv_confirm;
    private TextView number_00;
    private RelativeLayout rl_close;
    private Activity context;
    private int type;
    public X3KeyBoardDialog(@NonNull Activity context, int type) {
        super(context,R.style.DialogTheme);
        this.context = context;
        this.type = type;


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.x3_number_keyboard);
        initDialogWindow();
        setCanceledOnTouchOutside(true);
        initView();


    }

    private void initView() {
        number_1 =  findViewById(R.id.number_1);
        number_2 =  findViewById(R.id.number_2);
        number_3 =  findViewById(R.id.number_3);
        number_4 =  findViewById(R.id.number_4);
        number_5 =  findViewById(R.id.number_5);
        number_6 =  findViewById(R.id.number_6);
        number_7 =  findViewById(R.id.number_7);
        number_8 =  findViewById(R.id.number_8);
        number_9 =  findViewById(R.id.number_9);
        number_0 =  findViewById(R.id.number_0);
        number_00 = findViewById(R.id.number_00);
        rl_close =  findViewById(R.id.rl_close);
        rl_delete = findViewById(R.id.rl_delete);
        rl_add = findViewById(R.id.rl_add);
        tv_confirm =  findViewById(R.id.tv_confirm);
        number_1.setOnClickListener(this);
        number_2.setOnClickListener(this);
        number_3.setOnClickListener(this);
        number_4.setOnClickListener(this);
        number_5.setOnClickListener(this);
        number_6.setOnClickListener(this);
        number_7.setOnClickListener(this);
        number_8.setOnClickListener(this);
        number_9.setOnClickListener(this);
        number_0.setOnClickListener(this);
        number_00.setOnClickListener(this);
        rl_close.setOnClickListener(this);
        rl_delete.setOnClickListener(this);
        rl_add.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        if(type == 1){//确定
            tv_confirm.setText(context.getString(R.string.x3_apply_confirm));
        }else {//更新
            tv_confirm.setText(context.getString(R.string.x3_alone_again_submit));
        }

    }

    @Override
    public void dismiss() {
        super.dismiss();
        if(onKeyboardListener != null){
            onKeyboardListener.dismissDialog();
        }
    }

    @Override
    public void show() {
        super.show();



    }

    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay();

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        p.height = (int) (mScreenPoint.y * 0.35);
        p.width = mScreenPoint.x;
        dialogWindow.setAttributes(p);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.number_1) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberOne(number_1.getText().toString());
            }
        } else if (id == R.id.number_2) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberTwo(number_2.getText().toString());
            }
        } else if (id == R.id.number_3) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberThree(number_3.getText().toString());
            }
        } else if (id == R.id.number_4) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberFour(number_4.getText().toString());
            }
        } else if (id == R.id.number_5) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberFive(number_5.getText().toString());
            }
        } else if (id == R.id.number_6) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberSix(number_6.getText().toString());
            }
        } else if (id == R.id.number_7) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberSeven(number_7.getText().toString());
            }
        } else if (id == R.id.number_8) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberEight(number_8.getText().toString());
            }
        } else if (id == R.id.number_9) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberNine(number_9.getText().toString());
            }
        } else if (id == R.id.number_0) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberZero(number_0.getText().toString());
            }
        } else if (id == R.id.number_00) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.inputNumberZeros(number_00.getText().toString());
            }
        } else if (id == R.id.rl_close) {
            if (onKeyboardListener != null) {
                onKeyboardListener.close();
            }
        } else if (id == R.id.rl_delete) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.delete();
            }
        } else if (id == R.id.rl_add) {
            shock();
            if (onKeyboardListener != null) {
                onKeyboardListener.add();
            }
        } else if (id == R.id.tv_confirm) {
            if (onKeyboardListener != null) {
                onKeyboardListener.save();
            }
        }
    }

    public interface OnKeyboardListener{
        void inputNumberOne(String text);
        void inputNumberTwo(String text);
        void inputNumberThree(String text);
        void inputNumberFour(String text);
        void inputNumberFive(String text);
        void inputNumberSix(String text);
        void inputNumberSeven(String text);
        void inputNumberEight(String text);
        void inputNumberNine(String text);
        void inputNumberZero(String text);
        void inputNumberZeros(String text);
        void close();
        void delete();
        void add();
        void save();
        void dismissDialog();
    }
    public OnKeyboardListener onKeyboardListener;

    public void setOnKeyboardListener(OnKeyboardListener onKeyboardListener) {
        this.onKeyboardListener = onKeyboardListener;
    }

    public void shock(){
        VibrateHelp.vSimple(context, 60);
    }

}
